/**
 *====================================================
 * 文件名称: LogisticsClerkServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年09月23日	chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.member.service;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.core.exception.NotFoundException;
import com.yuexunit.finance.member.entity.LogisticsClerkEntity;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.DateUtil;

/**
 * @ClassName: LogisticsClerkServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年09月23日 18:55:49
 */
@Implements(LogisticsClerkService.class)
public class LogisticsClerkServiceImpl implements LogisticsClerkService {

	@Override
	@Transactional
	public Long createLogisticsClerk(LogisticsClerkEntity entity) {
		entity.setEnabled(Boolean.TRUE);
		entity.setCreateDateTime(DateUtil.now());
		entity.setUpdateDateTime(DateUtil.now());
		PersistUtil.insert(entity);
		return entity.getClerkId();
	}

	@Override
	@Transactional
	public void updateLogisticsClerk(LogisticsClerkEntity entity) {
		LogisticsClerkEntity persist = inqueryLogisticsClerkDetail(entity.getClerkId());
		persist.setClerkMobile(entity.getClerkMobile());
		persist.setClerkName(entity.getClerkName());
		persist.setUpdateDateTime(DateUtil.now());
		PersistUtil.update(persist);
	}

	@Override
	@Transactional
	public void disableLogisticsClerk(LogisticsClerkEntity entity) {
		entity.setEnabled(false);
		entity.setUpdateDateTime(DateUtil.now());
		PersistUtil.update(entity);
	}

	@Override
	public LogisticsClerkEntity inqueryLogisticsClerk(Long clerkId) {
		return PersistUtil.get(LogisticsClerkEntity.class, clerkId);
	}

	@Override
	public LogisticsClerkEntity inqueryLogisticsClerkDetail(Long clerkId) {
		LogisticsClerkEntity entity = inqueryLogisticsClerk(clerkId);
		if (entity == null || entity.getEnabled() == false) {
			throw new NotFoundException("该人员不存在！");
		}
		return entity;
	}

	@Override
	public PageList<LogisticsClerkEntity> inqueryLogisticsClerkList(Long companyId, String clerkName, String clerkMobile, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(LogisticsClerkEntity.class);
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		if (companyId != null) {
			criteria.add(Restrictions.eq("companyId", companyId));
		}
		if (StringUtils.isNotBlank(clerkName)) {
			criteria.add(Restrictions.like("clerkName", "%" + clerkName + "%"));
		}
		if (StringUtils.isNotBlank(clerkMobile)) {
			criteria.add(Restrictions.like("clerkMobile", "%" + clerkMobile + "%"));
		}
		return PersistUtil.get(criteria, page);
	}

	@Override
	public List<LogisticsClerkEntity> inqueryLogisticsClerkList(Long companyId, Long accountId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(LogisticsClerkEntity.class);
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		if (companyId != null) {
			criteria.add(Restrictions.eq("companyId", companyId));
		}
		if (accountId != null) {
			criteria.add(Restrictions.eq("accountId", accountId));
		}
		return PersistUtil.get(criteria);
	}

	@Override
	public LogisticsClerkEntity inqueryLogisticsClerk(Long logisticsCompanyId, String clerkMobile) {
		DetachedCriteria criteria = DetachedCriteria.forClass(LogisticsClerkEntity.class);
		criteria.add(Restrictions.eq("companyId", logisticsCompanyId));
		criteria.add(Restrictions.eq("clerkMobile", clerkMobile));
		List<LogisticsClerkEntity> list = PersistUtil.get(criteria);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

}
